package com.cat.greedyAlgorithm;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/advantage-shuffle/description/
 * @create 2025/8/24 16:59
 * @since JDK17
 */

public class Solution25 {
    public int[] advantageCount(int[] nums1, int[] nums2) {
        Arrays.sort(nums1);
        int n = nums1.length, k = 0;
        int[] ans = new int[n];
        int[][] h = new int[n][2];
        for (int i = 0; i < n; i++) {
            h[i][0] = nums2[i];
            h[i][1] = i;
        }
        // 按照值排升序
        Arrays.sort(h, (o1, o2) -> o1[0] - o2[0]);
        for (int i = 0, j = 0, m = n - 1; i < n; i++) {
            if (nums1[i] > h[j][0]) {
                ans[h[j][1]] = nums1[i];
                j++;
            } else {
                ans[h[m--][1]] = nums1[i];
            }
        }

        return ans;
    }
}
